package com.qf.blog.controller;

import com.qf.blog.commons.constant.RedisKeys;
import com.qf.blog.commons.utils.UserThreadLocal;
import com.qf.blog.entity.Comment;
import com.qf.blog.entity.Message;
import com.qf.blog.entity.User;
import com.qf.blog.service.CommentService;
import com.qf.blog.service.UserService;
import com.qf.blog.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.Set;

/**
 * User: key
 * ClassName:LetterController
 * Date: 2023/6/10
 * Time: 11:40
 * description:
 */
@Controller
@RequestMapping("/letter")
public class NoticeController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;


    /*
    * 系统消息的接口
    * 可以显示关注信息、点赞信息、评论信息
    * */
    @RequestMapping("/list")
    public String list(ModelMap modelMap) {
        ZSetOperations<String, String> zset = redisTemplate.opsForZSet();

        //获取当前登录用户
        User user = userService.getById(UserThreadLocal.get().getUserId());

        //获取和当前登录用户有关的redis所有中的key
        //1、关注和粉丝相关
        String followerKey = String.format(RedisKeys.USER_FOLLOWER_KEY,user.getId());
        //2、点赞相关
        //3、评论相关

        //4、检查redis中相应的key是否存在
        Boolean flag = redisTemplate.hasKey(followerKey);

        //获取当前登录用户的id
        //这里是错的，拿不到数据
        //要通过userId来获取

        /*
        * 这里我们需要如何来处理点赞的逻辑
        * 需要读取redis中的对应用户的key来统计点赞数量
        * */

        /*
        * 这里我们需要如何来处理评论的逻辑
        * 需要读取数据库中comment中对当前用户的帖子的回复
        * 根据当前用户的id在comment中查询
        * */
//        Comment comment = commentService.getById(user.getId());
//
//        if (!ObjectUtils.isEmpty(comment)) {
//            //创建一个NoticeVo
//            NoticeVo followNotice = new NoticeVo();
//            Message message = new Message();
//            message.setCreateTime(new Date());
//            followNotice.setMessage(message);
//            followNotice.setUser(userService.getById(comment.getUserId()));
//            followNotice.setEntityType(comment.getEntityType());
//            followNotice.setCount(Math.toIntExact(commentService.getCommentCount(comment.getEntityId())));
//            modelMap.put("commentNotice",followNotice);
//        }

        //5、如果存在则获取所有的
        //获取粉丝点击关注后发送的消息
        if(flag){
            //创建一个NoticeVo
            NoticeVo followNotice = new NoticeVo();
            Set<String> fans = zset.range(followerKey, 0, -1);
            Integer count = fans.size();
            followNotice.setCount(count);
            for (String fan : fans) {
                Integer id = Integer.valueOf(fan);
                User uFans = userService.getById(id);
                Double score = zset.score(followerKey, fan);
                long l = Math.round(score);
                Date date = new Date(l);
                Message message = new Message();
                message.setCreateTime(date);
                followNotice.setUser(uFans);
                followNotice.setMessage(message);
            }
            modelMap.put("followNotice",followNotice);
        }

        //获取被点赞后发送的消息
        //获取被评论后发送的消息
        return "site/notice";
    }
}
